package com.koocloud.electroplatemanage.service.impl;

import com.koocloud.electroplatemanage.auth.mapper.IUserDao;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.ReportFormMapper;
import com.koocloud.electroplatemanage.pojo.User;
import com.koocloud.electroplatemanage.pojo.reportformpojo.CompletionItemNumsVo;
import com.koocloud.electroplatemanage.pojo.reportformpojo.UnCompletionItemNumsVo;

import com.koocloud.electroplatemanage.mapper.ReportFormMapper;
import com.koocloud.electroplatemanage.pojo.reportformpojo.PersonUnCompletionItemNumsVo;
import com.koocloud.electroplatemanage.service.ReportFormService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

import static com.koocloud.electroplatemanage.constant.IncomePartJudgeSqlFieldConstant.TOTAL;
import static com.koocloud.electroplatemanage.constant.IncomePartJudgeSqlFieldConstant.ZERO;

/**
 * @program: electroplatemanage
 * @description: 报表service层实现类
 * @author: zww
 * @create: 2020-10-28 09:21
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ReportFormServiceImpl implements ReportFormService {
    @Resource
    private ReportFormMapper reportFormMapper;
    @Resource
    private IUserDao iUserDao;
    /**
     *@Description:完成情况统计项数
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: zww
     *@date: 2020/10/28
     */
    @Override
    public ResponseTemplate completionItemNums(String year) {
        try {
            //创建返回前端实体List
            List<CompletionItemNumsVo> list = new ArrayList<CompletionItemNumsVo>();
            if(StringUtils.isEmpty(year)) {//判断年份是否为空，空则取当年年份
                Calendar cal = Calendar.getInstance();
                //取当年年份
                year = String.valueOf(cal.get(Calendar.YEAR));

            }
            //设置开始时间，结束时间，开始停留时间，结束停留时间默认值
            String startDate = "0";
            String endDate = "0";
            String startResidenceTime = "0";
            String endResidenceTime = "0";

            //第一个循环代表12个月
                for (int i = 1; i <= 12; i++) {
                    CompletionItemNumsVo completionItemNumsVo = new CompletionItemNumsVo();
                    //设置月份
                    completionItemNumsVo.setMonth(String.valueOf(i));
                    //第二个循环代表七次停留时间查询区间（0、1-3、0-3合计、4-14、15-30、30以上、合计）
                    if (i == 1) {
                        startDate = String.valueOf(Integer.parseInt(year) - 1) + "-12-26 00:00:00";
                        endDate = year + "-01-25 23:59:59";
                        //设置时间区间
                        completionItemNumsVo.setCycleTime(String.valueOf(Integer.parseInt(year) - 1)+"/"+"12"
                                +"/"+"26"+"-"+year+"/"+"1"+"/"+"25");
                    } else {
                        startDate = year + "-" + String.format("%02d",(i-1)) + "-26 00:00:00";
                        endDate = year + "-" + String.format("%02d",i) + "-25 23:59:59";
                        //设置时间区间
                        completionItemNumsVo.setCycleTime(year+"/"+String.valueOf(i - 1)
                                +"/"+"26"+"-"+year+"/"+String.valueOf(i)+"/"+"25");
                    }
                    for(int j=1;j<=7;j++) {
                        if(j==1) {
                            String items = reportFormMapper.completionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                            if(StringUtils.isEmpty(items)) {
                                items = "0";
                            }
                            //设置0天完成项数
                            completionItemNumsVo.setZeroDay(items);
                        }
                        else if(j==2) {
                            startResidenceTime = "1";
                            endResidenceTime = "3";
                            String items = reportFormMapper.completionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                            if(StringUtils.isEmpty(items)) {
                                items = "0";
                            }
                            //设置1天到3天完成项数
                            completionItemNumsVo.setOneToThree(items);
                        }
                        else if(j==3) {
                            startResidenceTime = "0";
                            endResidenceTime = "3";
                            String items = reportFormMapper.completionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                            if(StringUtils.isEmpty(items)) {
                                items = "0";
                            }
                            //设置0天到3天合计完成项数
                            completionItemNumsVo.setSumZeroToThree(items);
                        }
                        else if(j==4) {
                            startResidenceTime = "4";
                            endResidenceTime = "14";
                            String items = reportFormMapper.completionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                            if(StringUtils.isEmpty(items)) {
                                items = "0";
                            }
                            //设置4天到14天完成项数
                            completionItemNumsVo.setFourToFourteenDay(items);
                        }
                        else if(j==5) {
                            startResidenceTime = "15";
                            endResidenceTime = "30";
                            String items = reportFormMapper.completionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                            if(StringUtils.isEmpty(items)) {
                                items = "0";
                            }
                            //设置15天到30天完成项数
                            completionItemNumsVo.setFifteenToThirtyDay(items);
                        }
                        else if(j==6) {
                            startResidenceTime = "31";
                            endResidenceTime = "366";
                            String items = reportFormMapper.completionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                            if(StringUtils.isEmpty(items)) {
                                items = "0";
                            }
                            //设置30天以上完成项数
                            completionItemNumsVo.setThirtyDayAbove(items);
                        }
                        else {
                            //设置合计完成项数
                            int total = Integer.parseInt(completionItemNumsVo.getSumZeroToThree())//0到3天
                                    +Integer.parseInt(completionItemNumsVo.getFourToFourteenDay())//4到14天
                                    +Integer.parseInt(completionItemNumsVo.getFifteenToThirtyDay())//15到30天
                                    +Integer.parseInt(completionItemNumsVo.getThirtyDayAbove());//30天以上
                            completionItemNumsVo.setTotal(String.valueOf(total));
                        }
                    }
                    //添加记录实体
                    list.add(completionItemNumsVo);
                }

            return ResponseTemplate.builder().code(0).message("成功").data(list).count(12l).build();
        }
         catch (Exception e) {
            e.printStackTrace();
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
    /**
     *@Description:未完成情况统计项数
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: zww
     *@date: 2020/10/28
     */
    @Override
    public ResponseTemplate unCompletionItemNums(String year) {
        try {
            //创建返回前端实体List
            List<UnCompletionItemNumsVo> list = new ArrayList<UnCompletionItemNumsVo>();
            if(StringUtils.isEmpty(year)) {//判断年份是否为空，空则取当年年份
                Calendar cal = Calendar.getInstance();
                //取当年年份
                year = String.valueOf(cal.get(Calendar.YEAR));

            }
            //设置开始时间，结束时间，开始停留时间，结束停留时间默认值
            String startDate = "0";
            String endDate = "0";
            String startResidenceTime = "0";
            String endResidenceTime = "0";

            //第一个循环代表12个月
            for (int i = 1; i <= 12; i++) {
                UnCompletionItemNumsVo unCompletionItemNumsVo = new UnCompletionItemNumsVo();
                //设置月份
                unCompletionItemNumsVo.setMonth(String.valueOf(i));
                //第二个循环代表七次停留时间查询区间（0、1-3、0-3合计、4-14、15-30、30以上、合计）
                if (i == 1) {
                    startDate = String.valueOf(Integer.parseInt(year) - 1) + "-12-26 00:00:00";
                    endDate = year + "-1-25 23:59:59";
                    //设置时间区间
                    unCompletionItemNumsVo.setCycleTime(String.valueOf(Integer.parseInt(year) - 1)+"/"+"12"
                            +"/"+"26"+"-"+year+"/"+"01"+"/"+"25");
                } else {
                    startDate = year + "-" + String.format("%02d",(i-1)) + "-26 00:00:00";
                    endDate = year + "-" + String.format("%02d",i) + "-25 23:59:59";
                    //设置时间区间
                    unCompletionItemNumsVo.setCycleTime(year+"/"+String.valueOf(i - 1)
                            +"/"+"26"+"-"+year+"/"+String.valueOf(i)+"/"+"25");
                }
                for(int j=1;j<=14;j++) {
                    if(j==1) {
                        startResidenceTime = "-1";
                        endResidenceTime = "0";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置0天未完成项数
                        unCompletionItemNumsVo.setZeroDay(items);
                    }
                    else if(j==2) {
                        startResidenceTime = "-2";
                        endResidenceTime = "-1";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置1天未完成项数
                        unCompletionItemNumsVo.setOneDay(items);
                    }
                    else if(j==3) {
                        startResidenceTime = "-3";
                        endResidenceTime = "-2";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置2天未完成项数
                        unCompletionItemNumsVo.setTwoDay(items);
                    }
                    else if(j==4) {
                        startResidenceTime = "-4";
                        endResidenceTime = "-3";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置3天未完成项数
                        unCompletionItemNumsVo.setThreeDay(items);
                    }
                    else if(j==5) {
                        startResidenceTime = "-5";
                        endResidenceTime = "-4";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置4天未完成项数
                        unCompletionItemNumsVo.setFourDay(items);
                    }
                    else if(j==6) {
                        startResidenceTime = "-6";
                        endResidenceTime = "-5";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置5天未完成项数
                        unCompletionItemNumsVo.setFiveDay(items);
                    }
                    else if(j==7) {
                        startResidenceTime = "-7";
                        endResidenceTime = "-6";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置6天未完成项数
                        unCompletionItemNumsVo.setSixDay(items);
                    }
                    else if(j==8) {
                        startResidenceTime = "-8";
                        endResidenceTime = "-7";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置7天未完成项数
                        unCompletionItemNumsVo.setSevenDay(items);
                    }
                    else if(j==9) {
                        startResidenceTime = "-14";
                        endResidenceTime = "-8";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置8天到13天未完成项数
                        unCompletionItemNumsVo.setEightToThirteenDay(items);
                    }
                    else if(j==10) {
                        startResidenceTime = "-15";
                        endResidenceTime = "-14";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置14天未完成项数
                        unCompletionItemNumsVo.setFourteenDay(items);
                    }
                    else if(j==11) {
                        startResidenceTime = "-21";
                        endResidenceTime = "-15";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置15天到20天未完成项数
                        unCompletionItemNumsVo.setFifteenToTwenty(items);
                    }
                    else if(j==12) {
                        startResidenceTime = "-31";
                        endResidenceTime = "-21";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置21天到30天未完成项数
                        unCompletionItemNumsVo.setTwentyOneToThirtyDay(items);
                    }
                    else if(j==13) {
                        startResidenceTime = "-366";
                        endResidenceTime = "-31";
                        String items = reportFormMapper.unCompletionItemNums(startDate,endDate,startResidenceTime,endResidenceTime);
                        if(StringUtils.isEmpty(items)) {
                            items = "0";
                        }
                        //设置30天以上未完成项数
                        unCompletionItemNumsVo.setThirtyOneDayAbove(items);
                    }
                    else {
                        //设置合计完成项数0天+1天+2天+...+30天以上
                        int total = Integer.parseInt(unCompletionItemNumsVo.getZeroDay())//0天
                                +Integer.parseInt(unCompletionItemNumsVo.getOneDay())//1天
                                +Integer.parseInt(unCompletionItemNumsVo.getTwoDay())//2天
                                +Integer.parseInt(unCompletionItemNumsVo.getThreeDay())//3天
                                +Integer.parseInt(unCompletionItemNumsVo.getFourDay())//4天
                                +Integer.parseInt(unCompletionItemNumsVo.getFiveDay())//5天
                                +Integer.parseInt(unCompletionItemNumsVo.getSixDay())//6天
                                +Integer.parseInt(unCompletionItemNumsVo.getSevenDay())//7天
                                +Integer.parseInt(unCompletionItemNumsVo.getEightToThirteenDay())//8-13天
                                +Integer.parseInt(unCompletionItemNumsVo.getFourteenDay())//14天
                                +Integer.parseInt(unCompletionItemNumsVo.getFifteenToTwenty())//15-20天
                                +Integer.parseInt(unCompletionItemNumsVo.getTwentyOneToThirtyDay())//21-30天
                                +Integer.parseInt(unCompletionItemNumsVo.getThirtyOneDayAbove());//31天以上
                        //设置合计
                        unCompletionItemNumsVo.setTotal(String.valueOf(total));
                    }
                }
                //添加记录实体
                list.add(unCompletionItemNumsVo);
            }

            return ResponseTemplate.builder().code(0).message("成功").data(list).count(12l).build();
        }
        catch (Exception e) {
            e.printStackTrace();
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
    /**
     *@Description:人员未完成情况统计项数
     *@Param: [year]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: zww
     *@date: 2020/10/28
     */
    @Override
    public ResponseTemplate personUnCompletionItemNums(String year) {
        try {
            String startYearTime = String.valueOf(Integer.valueOf(year)-1);
            String startDate = startYearTime + "-12-26 00:00:00";
            String endDate = year + "-12-25 23:59:59";
            //查看人员
            List<PersonUnCompletionItemNumsVo> personList = reportFormMapper.selectOperator();
            //获取总数
            List<PersonUnCompletionItemNumsVo> list = reportFormMapper.personUnCompletionItemNums(startDate,endDate);
            //停留7天的
            List<PersonUnCompletionItemNumsVo> listStaySeven = reportFormMapper.personUnCompletionItemNumsStaySeven(startDate,endDate);
            //停留15天的
            List<PersonUnCompletionItemNumsVo> listStayFifteen = reportFormMapper.personUnCompletionItemNumsStayFifteen(startDate,endDate);
            //
            int sumCount = 0;
            int sumStaySeven = 0;
            int sumStayFifteen = 0;
            for (int i = 0 ; i < personList.size() ; i++){
                for (PersonUnCompletionItemNumsVo personUnCompletionItemNumsVo: list ) {
                    if (personList.get(i).getName().equals(personUnCompletionItemNumsVo.getName())){
                        personList.get(i).setYearUnCompletionItemNums(personUnCompletionItemNumsVo.getYearUnCompletionItemNums());
                        personList.get(i).setTypeWorkTotal(personUnCompletionItemNumsVo.getTypeWorkTotal());
                    }else {
                        continue;
                    }
//                    if (StringUtils.isEmpty(personList.get(i).getTypeWorkTotal())){
//                        personList.get(i).setSevenDayAbove(ZERO);
//                    }
                }
                if (StringUtils.isEmpty(personList.get(i).getYearUnCompletionItemNums())){
                    personList.get(i).setYearUnCompletionItemNums(ZERO);
                }
                for (PersonUnCompletionItemNumsVo personUnCompletionItemNumsVoStaySeven: listStaySeven ) {
                    if (personList.get(i).getName().equals(personUnCompletionItemNumsVoStaySeven.getName())){
                        personList.get(i).setSevenDayAbove(personUnCompletionItemNumsVoStaySeven.getSevenDayAbove());
                    }else {
                        continue;
                    }
                }
                if (StringUtils.isEmpty(personList.get(i).getSevenDayAbove())){
                    personList.get(i).setSevenDayAbove(ZERO);
                }
                for (PersonUnCompletionItemNumsVo personUnCompletionItemNumsVoStayFifteen: listStayFifteen ) {
                    if (personList.get(i).getName().equals(personUnCompletionItemNumsVoStayFifteen.getName())){
                        personList.get(i).setFifteenDayAbove(personUnCompletionItemNumsVoStayFifteen.getFifteenDayAbove());
                    }else {
                        continue;
                    }
                }
                if (StringUtils.isEmpty(personList.get(i).getFifteenDayAbove())){
                    personList.get(i).setFifteenDayAbove(ZERO);
                }
                sumCount += Integer.valueOf(personList.get(i).getYearUnCompletionItemNums());
                sumStaySeven += Integer.valueOf(personList.get(i).getSevenDayAbove());
                sumStayFifteen += Integer.valueOf(personList.get(i).getFifteenDayAbove());
                StringBuilder operators = new StringBuilder();
                if (!StringUtils.isEmpty(personList.get(i).getName()) && personList.get(i).getName().contains("、")){
                    String[] splits = personList.get(i).getName().split("、");
                    for (int j = 0; j < splits.length; j++) {
                        User user = iUserDao.selectByJobNum(splits[j]);
                        if (j == 0){
                            operators.append(user.getFullname());
                        }else {
                            operators.append("、").append(user.getFullname());
                        }
                    }
                }else {
                    User user = iUserDao.selectByJobNum(personList.get(i).getName());
                    operators.append(user.getFullname());
                }
                personList.get(i).setName(String.valueOf(operators));
            }
            PersonUnCompletionItemNumsVo personUnCompletionItemNumsVo = new PersonUnCompletionItemNumsVo();
            personUnCompletionItemNumsVo.setName(TOTAL);
            personUnCompletionItemNumsVo.setYearUnCompletionItemNums(String.valueOf(sumCount));
            personUnCompletionItemNumsVo.setSevenDayAbove(String.valueOf(sumStaySeven));
            personUnCompletionItemNumsVo.setFifteenDayAbove(String.valueOf(sumStayFifteen));
            personList.add(personUnCompletionItemNumsVo);

            return ResponseTemplate.builder().code(0).message("成功").count(Long.valueOf(personList.size())).data(personList).build();
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }
}